<!--# include virtual="/admin/common/html/header.html"-->
<body>
<nav class="breadcrumb">
	<i class="Hui-iconfont">&#xe67f;</i> 首页 
	<span class="c-gray en">&gt;</span> 造价指标
	<span class="c-gray en">&gt;</span>指标分类
	<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" >
		<i class="Hui-iconfont">&#xe68f;</i>
	</a>
</nav>
<section class="Hui-article-box">
	<div style="margin-top: 10px; margin-left: 10px;">
		<ul id="treeDemo" class="ztree"></ul>
	</div>
</div>
</section>
<!--# include virtual="/admin/common/html/_footer.html"-->
<script type="text/javascript">
var setting = {
	view: {
		dblClickExpand: true,
		showLine: true,
		selectedMulti: true,
		addHoverDom: addHoverDom
	},
	data: {
		simpleData: {
			enable:true,
			idKey: "catalogId",
			pIdKey: "parentId",
			rootPId: "0"
		},
		key:{
			name: "catalogName"
		}
	},
	edit: {
		enable: true,
		editNameSelectAll: true,
		showRemoveBtn: true,
		removeTitle: "点击删除节点",
		showRenameBtn: true,
		renameTitle: "点击编辑节点",
		drag: {
			isCopy: false,
			isMove: true
		}
	},
	callback: {
		beforeDrag: beforeDrag,
		beforeEditName: beforeEditName,
		beforeRemove: beforeRemove,
		beforeRename: beforeRename,
		onRemove: onRemove,
		onRename: onRename

	}
};
var  zTree = null;
$.post('/guidance_service/front/data/catalog/search.do',{},function(obj){
	var data = obj.data;
	var list = new Array();
	var t = $("#treeDemo");
	zTree = $.fn.zTree.init(t, setting, data);
});
var log, className = "dark";
function beforeDrag(treeId, treeNodes) {
	return false;
}
function beforeEditName(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	showLog("[ "+getTime()+" beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.selectNode(treeNode);
	zTree.editName(treeNode);
	layer.myprompt(
		{"title": '修改节点，并确认',
		 "promName":["名称","编码","使用性质（逗号隔开）","经济指标"],
		 "value":[treeNode.catalogName,treeNode.catalogCode,
				  treeNode.characteristics,treeNode.economicIndicators]}, 
		 function(text, index){
			var data ={
				"catalogName":text[0].trim(),
				"catalogCode":text[1].trim(),
				"characteristics":text[2].trim(),
				"economicIndicators":text[3].trim(),
				"catalogId":treeNode.catalogId
			}
			$.post("/guidance_service/front/data/catalog/update.do",data,function(result){
				if(result.resultCode == 200){
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
					treeNode.catalogName = text[0].trim();
					treeNode.catalogCode =text[1].trim();
				    treeNode.characteristics =text[2].trim();
				    treeNode.economicIndicators =text[3].trim();
				    zTree.refresh();
				    layer.close(index);
				}else{
					layer.msg(result.message,{icon:2,time:1000},function(){
						layer.close(index);
					});
				}
			});
		});
	return false;
}
function beforeRemove(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.selectNode(treeNode);
	layer.confirm('确认删除-'+treeNode.catalogName+'-节点？', {
		  btn: ['确定','取消'] //按钮
		}, function(index){
			$.post("/guidance_service/front/data/catalog/logicDelete.do",{"catalogId":treeNode.catalogId},function(result){
		    	zTree.removeNode(treeNode);
		    	zTree.refresh();
		    	layer.close(index);
			});	
		}, function(index){
			 layer.close(index);
		});
	return false;
}
function onRemove(e, treeId, treeNode) {
	showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	return false;
}
function beforeRename(treeId, treeNode, newName, isCancel) {
	console.dir(treeNode);
	console.dir(newName);
	console.dir(isCancel);
	className = (className === "dark" ? "":"dark");
	showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
	if (newName.length == 0) {
		setTimeout(function() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.cancelEditName();
			alert("节点名称不能为空.");
		}, 0);
		return false;
	}
//	var  data = {
//		catalogId:treeNode.catalogId,
//		catalogName: newName,
//	}
//	$.post("/guidance_service/front/costIndex/catalog/update.do",data,function(result){
//		console.dir(result);
//	});
	return true;
}
function onRename(e, treeId, treeNode, isCancel) {
	showLog((isCancel ? "<span style='color:red'>":"") + "[ "+getTime()+" onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name + (isCancel ? "</span>":""));
}
function showRemoveBtn(treeId, treeNode) {
	return !treeNode.isFirstNode;
}
function showRenameBtn(treeId, treeNode) {
	return !treeNode.isLastNode;
}
function showLog(str) {
	if (!log) log = $("#log");
	log.append("<li class='"+className+"'>"+str+"</li>");
	if(log.children("li").length > 8) {
		log.get(0).removeChild(log.children("li")[0]);
	}
}

var newCount = 1;
function addHoverDom(treeId, treeNode) {
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
		+ "' title='新增节点' onfocus='this.blur();'></span>";
	$("#" + treeNode.tId + "_span").after(addStr);
	var btn = $("#addBtn_"+treeNode.tId);
	if (btn) btn.bind("click", function(){
		layer.myprompt({title: '输入节点名称，并确认',"promName":["名称","编码","使用性质（逗号隔开）","经济指标"]}, function(text, index){
			var data ={
				"catalogName":text[0].trim(),
				"catalogCode":text[1].trim(),
				"characteristics":text[2].trim(),
				"economicIndicators":text[3].trim(),
				"parentId":treeNode.catalogId
			}
			$.post("/guidance_service/front/costIndex/catalog/add.do",data,function(result){
				if(result.resultCode == 200){
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				    zTree.addNodes(treeNode, {catalogId:result.data.catalogId, parentId:result.data.parentId, catalogName:result.data.catalogName,catalogCode:result.data.catalogCode});
				    layer.close(index);
				}else{
					layer.msg(result.message,{icon:2,time:1000},function(){
						layer.close(index);
					});
				}
			});
		});
		return false;
	});
};

function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.tId).unbind().remove();
};
function selectAll() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.setting.edit.editNameSelectAll =  $("#selectAll").attr("checked");
}

$(document).ready(function(){
	$("#selectAll").bind("click", selectAll);
});
</script>
</body>
</html>